package crp.services.api.graphql;

import com.qwlabs.graphql.relay.Connection;
import com.qwlabs.graphql.relay.Relays;
import crp.core.student.app.StudentService;
import crp.services.api.graphql.models.StudentPayload;
import org.eclipse.microprofile.graphql.DefaultValue;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;

import javax.annotation.security.PermitAll;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.validation.constraints.NotNull;

@GraphQLApi
@RequestScoped
@Transactional
public class StudentGraphQLApi {
    private final StudentService studentService;

    @Inject
    public StudentGraphQLApi(StudentService studentService) {
        this.studentService = studentService;
    }

    @Query
    @PermitAll
    @NotNull
    public Connection<StudentPayload> students(
            String courseId,
            @DefaultValue("0") String after,
            @DefaultValue("10") Integer first) {
        return Relays.of(studentService.searcher()
                        .findBy(courseId, Relays.of(after, first)),
                StudentPayload::of);
    }

}